// goods/pages/submitOrderSuccess/submitOrderSuccess.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxParse = require('../../wxParse/wxParse.js');
const utils = require('../../../utils/util.js');
const app = getApp();
var interval = null //倒计时函数
var againinterval = null //倒计时函数
var orderinterval = null //倒计时函数
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTime: 5,
    time: '5秒后返回',
    orderInfo: {},
    type: null,
    resultMsg: '支付中...'
  },

  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒后返回'
      })
      if (currentTime <= 0) {
        clearInterval(interval);
        clearInterval(againinterval);
        clearInterval(orderinterval);
        if (that.data.type == 1) {
          wx.redirectTo({
            url: `/goods/pages/order/order`
          })
        } else {
          let pages = getCurrentPages(); //获取小程序页面栈
          let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
          beforePage.getData(); //触发上个页面自定义的shuaxin方法
          wx.navigateBack({
            delta: 1
          })
        }

      }
    }, 1000)
  },
  // toback() {
  //   const that = this;
  //   clearInterval(interval);
  //   clearInterval(againinterval);
  //   clearInterval(orderinterval);
  //   if (that.data.type == 1) {
  //     wx.redirectTo({
  //       url: `/goods/pages/order/order`
  //     })
  //   } else {
  //     wx.navigateBack({
  //       delta: 1
  //     })
  //   }
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      type: options.type
    })
    login.checkLogin().then(res => {
      that.getProvince(options.orderSn);
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getProvince(options.orderSn);
      }
    })
  },
  getProvince(e) {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
      master_order_sn: e
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getOrderByMasertSN, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.getCode();
        againinterval = setInterval(function () {
          that.aginGetdata(e);
        }, 2000) //单位ms，会不断调用
        let resultMsg = ''
        if (res.data.result.order.pay_status == 1) {
          resultMsg = '支付成功';
        } else {
          resultMsg = '支付中...';
        }
        that.setData({
          orderInfo: res.data.result.order,
          resultMsg
        })
      } else {
        wx.showToast({
          title: res.data.msg
        })
      }
      that.setData({
        hiddenLoading: true
      })
    })
  },

  aginGetdata(e) {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
      master_order_sn: e
    }
    myRequest.EasyHttp.post(api.reqUrls.getOrderByMasertSN, data).then(res => {
      if (res.data.status == 1) {
        let resultMsg = ''
        if (res.data.result.order.pay_status == 1) {
          resultMsg = '支付成功';
        } else {
          resultMsg = '支付中...';
        }
        that.setData({
          orderInfo: res.data.result.order,
          resultMsg
        })
      } else {
        wx.showToast({
          title: res.data.msg
        })
      }
      that.setData({
        hiddenLoading: true
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(interval);
    clearInterval(againinterval);
    clearInterval(orderinterval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})